<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta name="misapplication-tap-highlight" content="no"/>
    <meta name="HandheldFriendly" content="true"/>
    <meta name="MobileOptimized" content="320"/>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" type="text/css" href="/newera/sources/XspWeb/Client/Scripts/Mui/css/mui.css?v=<?php echo SITE_VERSION;?>"/>
    <link rel="stylesheet" type="text/css" href="/newera/sources/Addons/WeVote/View/default/Public/css/page.css?v=<?php echo SITE_VERSION;?>"/>
    <script type="text/javascript" src="/newera/sources/XspWeb/Client/Scripts/Mui/js/mui.js?v=<?php echo SITE_VERSION;?>"></script>
    <script type="text/javascript" src="/newera/sources/XspWeb/Client/Scripts/MobileUI/js/mui.js?v=<?php echo SITE_VERSION;?>"></script>
    <script type="text/javascript" src="/newera/sources/XspWeb/Client/Scripts/Core/Common-0.2.js?v=<?php echo SITE_VERSION;?>"></script>
    <script type="text/javascript" src="/newera/sources/XspWeb/Client/Scripts/Core/AResult.js?v=<?php echo SITE_VERSION;?>"></script>
    <script type="text/javascript" src="/newera/sources/XspWeb/Client/Scripts/Core/OO-0.2.js?v=<?php echo SITE_VERSION;?>"></script>
    <script type="text/javascript" src="/newera/sources/XspWeb/Client/Scripts/MobileUI/js/Page.js?v=<?php echo SITE_VERSION;?>"></script>
    <script type="text/javascript"
            src="/newera/sources/XspWeb/Client/Scripts/MobileUI/js/InitPage.js?v=<?php echo SITE_VERSION;?>"></script>
    <script src="/newera/sources/XspWeb/Client/Scripts/Mui/js/mui.pullToRefresh.js?v=<?php echo SITE_VERSION;?>"></script>
    <script src="/newera/sources/XspWeb/Client/Scripts/Mui/js/mui.pullToRefresh.material.js?v=<?php echo SITE_VERSION;?>"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script src="/newera/sources/XspWeb/Client/Scripts/MobileUI/js/Control.js?v=<?php echo SITE_VERSION;?>"></script>
    <script src="/newera/sources/XspWeb/Client/Scripts/MobileUI/js/Popup.js?v=<?php echo SITE_VERSION;?>"></script>
    <title></title>
    <style>
        .mui-pull-bottom-tips{
            display: none;
        }
    </style>
</head>
<body>
<!-- 图册 -->
<div id="slider" class="mui-slider">
    <div class="mui-slider-group mui-slider-loop">
        <div class="mui-slider-item mui-slider-item-duplicate">
        </div>
        <?php if(is_array($headpic)): $i = 0; $__LIST__ = $headpic;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$hp): $mod = ($i % 2 );++$i;?><div class="mui-slider-item">
                <a href="<?php echo ($hp["picurl"]); ?>"><img src="/newera/sources<?php echo get_cover_url($hp['picid']);?> "></a>
            </div><?php endforeach; endif; else: echo "" ;endif; ?>
        <div class="mui-slider-item mui-slider-item-duplicate">
        </div>
    </div>
    <div class="mui-slider-indicator">
        <div class="mui-indicator mui-active"></div>
        <div class="mui-indicator"></div>
        <div class="mui-indicator"></div>
        <div class="mui-indicator"></div>
    </div>
</div>
<!-- 动态数据 -->
<ul class="mui-table-view mui-grid-view mui-grid-9 white-bg">
    <li class="mui-table-view-cell mui-media mui-col-xs-4">
        <a href="#">
            <span>参与选手</span>
            <div class="mui-media-body"><?php echo ($candidatesCount); ?></div>
        </a>
    </li>
    <li class="mui-table-view-cell mui-media mui-col-xs-4">
        <a href="#">
            <span>累计投票</span>
            <div class="mui-media-body"><?php echo ($voteCount); ?></div>
        </a>
    </li>
    <li class="mui-table-view-cell mui-media mui-col-xs-4">
        <a href="#">
            <span>访问量</span>
            <div class="mui-media-body"><?php echo ($visitCount); ?></div>
        </a>
    </li>

</ul>
<!-- 点击列表 -->
<ul class="mui-table-view click-list">
    <li class="mui-table-view-cell">活动介绍:<?php echo ($introduce); ?></li>
    <li class="mui-table-view-cell">活动日期:<?php echo ($datetime); ?></li>
    <li class="mui-table-view-cell registration"><a href="javascript:void(0);" class="voteInfoButton">我的投票记录</a></li>
    <li class="mui-table-view-cell registration"><a href="<?php echo addons_url('WeVote://Home/registration', array('action_id' => $action_id));?>">我要报名</a></li>
    <li class="mui-table-view-cell registration"><a href="<?php echo ($shareUrl); ?>">关注微信号</a></li>
</ul>
<!-- 选项卡 -->
<div id="segmentedControl" class="mui-segmented-control">
    <a class="mui-control-item latestA mui-active" href="javascript:void(0)">
        最新
    </a>
    <a class="mui-control-item allA" href="javascript:void(0)">
        全部
    </a>
</div>
<!-- 瀑布布局 -->
<div class="mui-content-padded mui-slider-group2">
    <input type="hidden" name="currentPage" value="<?php echo ($currentPage); ?>" id="currentPage">
    <input type="hidden" name="currentType" value="<?php echo ($type); ?>" id="currentType">
    <input type="hidden" name="currentActionId" value="<?php echo ($action_id); ?>" id="currentActionId">
    <div id="item1mobile" class="mui-control-content mui-scroll2 mui-active">
        <ul class="mui-table-view mui-grid-view transparent-list mui-table-view2" id="mui-table-view2">
            <?php if(is_array($candidatesData)): $i = 0; $__LIST__ = $candidatesData;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?><li class="mui-table-view-cell mui-media list-width">
                    <a href="<?php echo addons_url('WeVote://Home/detail', array('action_id' => $action_id, 'id' => $vo['id']));?>">
                        <img class="mui-media-object" src="<?php echo ($vo["picture"]); ?>">

                        <div class="mui-media-body list-title"><?php echo ($vo["title"]); ?></div>
                    </a>
                    <div>
                        <button class="bt-list VoteButton">
                            <input type="hidden" name="id" class="id" value="<?php echo ($vo["id"]); ?>">
                            <input type="hidden" name="count" class="count" value="<?php echo ($vo["count"]); ?>">
                            投票
                        </button>
                        <span class="bt-data countClass"><?php echo ($vo["count"]); ?>票</span>
                    </div>

                </li><?php endforeach; endif; else: echo "" ;endif; ?>
        </ul>
    </div>
</div>
<div class="distance">
</div>

<div id="popover-topData" class="mui-popover topDataClass">
    <div class="mui-scroll-wrapper">
        <div class="mui-scroll"></div>
    </div>
</div>

<div id="popover-voteInfo" class="mui-popover voteInfoClass">
    <div class="mui-scroll-wrapper">
        <div class="mui-scroll"></div>
    </div>
</div>

<nav class="mui-bar mui-bar-tab">
    <a class="mui-tab-item mui-active" href="#tabbar">
        <span class="mui-icon mui-icon-phone"></span>
        <span class="mui-tab-label">电话</span>
    </a>
    <a class="mui-tab-item searchButton" href="#tabbar-with-chat">
        <span class="mui-icon mui-icon-chat"></span>
        <span class="mui-tab-label">搜索</span>
    </a>
    <a class="mui-tab-item topButton" href="javascript:void(0);">
        <span class="mui-icon mui-icon-list"></span>
        <span class="mui-tab-label">排行</span>
    </a>
    <a class="mui-tab-item" href="#tabbar-with-map">
        <span class="mui-icon mui-icon-star"></span>
        <span class="mui-tab-label">分享</span>
    </a>
</nav>
</body>
<script type="text/javascript">
    function InitializeComponent() {
        var gallery = mui('.mui-slider');
        gallery.slider({
            interval: 2000//自动轮播周期，若为0则不自动播放，默认为0；
        });

        var latestA = document.getElementsByClassName('latestA')[0];
        var allA = document.getElementsByClassName('allA')[0];
        // 设置当前的类型
        var currentType = document.getElementById('currentType');
        if(currentType.value == 'latest'){
            latestA.className = 'mui-control-item latestA mui-active';
            allA.className = 'mui-control-item allA';
        }
        else{
            latestA.className = 'mui-control-item latestA';
            allA.className = 'mui-control-item allA mui-active';
        }

        // 切换全部和最新修改type
        latestA.addEventListener('tap', function(){
            var url = "<?php echo addons_url('WeVote://Home/index');?>";
            var actionId = document.getElementById('currentActionId');
            location.href = url + "&type=latest&action_id=" + actionId.value;
        });
        allA.addEventListener('tap', function(){
            var url = "<?php echo addons_url('WeVote://Home/index');?>";
            var actionId = document.getElementById('currentActionId');
            location.href = url + "&type=all&action_id=" + actionId.value;
        });
        // 绑定投票按钮单击击事件
        var voteButtons = document.getElementsByClassName('VoteButton');
        var length = voteButtons.length;
        for(var i = 0; i < length; i ++){
            voteButtons[i].addEventListener('tap', voteButtonHandler);
        }

        // 绑定搜索按钮单击事件
        document.getElementsByClassName("searchButton")[0].addEventListener('tap', function(e) {
            e.detail.gesture.preventDefault(); //修复iOS 8.x平台存在的bug，使用plus.nativeUI.prompt会造成输入法闪一下又没了
            var actionId = document.getElementById('currentActionId');
            var url = "<?php echo addons_url('WeVote://Home/index');?>" + "&action_id=" + actionId.value;
            mui.prompt('请输入您要搜索的内容：', function(e) {
                if (e.index == 0) {
                    location.href = url + "&query=" + e.value;
                }
            })
        });

        // 排行榜弹出框控件
        var topPopup = new XspWeb.Controls.MobileUI.Popup('popover-topData');
        // 绑定排行按钮单击事件
        document.getElementsByClassName("topButton")[0].addEventListener('tap', function(e) {
            mui.toast('排行信息正在加载中....');
            topPopup.Clear();
            var url = "<?php echo addons_url('WeVote://Home/getTopAction');?>";
            var actionId = document.getElementById('currentActionId');
            var data = {
                action_id: actionId.value
            };

            // 表头
            var ul = document.createElement('ul');
            ul.className = 'mui-table-view mui-grid-view mui-grid-9';

            li = document.createElement('li');
            li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
            li.innerHTML = '排名';
            ul.appendChild(li);

            li = document.createElement('li');
            li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
            li.innerHTML = '姓名';
            ul.appendChild(li);

            li = document.createElement('li');
            li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
            li.innerHTML = '得票数';
            ul.appendChild(li);
            topPopup.Add(ul);
            $.getJSON(url, data, function(result){
                ul = document.createElement('ul');
                ul.className = 'mui-table-view mui-grid-view mui-grid-9 topData';
                var li, textNode;
                for(var i = 0; i < result.topData.length; i++){
                    li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
                    textNode = document.createTextNode(i+1);
                    li.appendChild(textNode);
                    ul.appendChild(li);

                    li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
                    textNode = document.createTextNode(result.topData[i].title);
                    li.appendChild(textNode);
                    ul.appendChild(li);

                    li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
                    textNode = document.createTextNode(result.topData[i].count);
                    li.appendChild(textNode);
                    ul.appendChild(li);
                }
                topPopup.Add(ul);
                topPopup.SetIsCentered(true);
                topPopup.Show();
            });
        });

        // 投票信息弹出框控件
        var voteInfoPopup = new XspWeb.Controls.MobileUI.Popup('popover-voteInfo');
        // 绑定投票信息按钮单击事件
        document.getElementsByClassName("voteInfoButton")[0].addEventListener('tap', function(e) {
            mui.toast('投票信息正在加载中....');
            voteInfoPopup.Clear();
            var url = "<?php echo addons_url('WeVote://Home/GetVoteInfoAction');?>";
            var actionId = document.getElementById('currentActionId');
            var data = {
                action_id: actionId.value
            };

            // 表头
            var ul = document.createElement('ul');
            ul.className = 'mui-table-view mui-grid-view mui-grid-9';

            li = document.createElement('li');
            li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
            li.innerHTML = '序号';
            ul.appendChild(li);

            li = document.createElement('li');
            li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
            li.innerHTML = '姓名';
            ul.appendChild(li);

            li = document.createElement('li');
            li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
            li.innerHTML = '票数';
            ul.appendChild(li);
            voteInfoPopup.Add(ul);

            ul = document.createElement('ul');
            ul.className = 'mui-table-view mui-grid-view mui-grid-9 topData';
            $.getJSON(url, data, function(result){
                var li, textNode;
                for(var i = 0; i < result.data.length; i++){
                    li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
                    textNode = document.createTextNode(i+1);
                    li.appendChild(textNode);
                    ul.appendChild(li);

                    li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
                    textNode = document.createTextNode(result.data[i].title);
                    li.appendChild(textNode);
                    ul.appendChild(li);

                    li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-4';
                    textNode = document.createTextNode(result.data[i].num);
                    li.appendChild(textNode);
                    ul.appendChild(li);
                }
                voteInfoPopup.Add(ul);
                voteInfoPopup.SetIsCentered(true);
                voteInfoPopup.Show();
            });
        });

        // 是否正在上拉或者下拉加载中
        var isPullLoading = false;

        //循环初始化所有下拉刷新，上拉加载。
        $.each(document.querySelectorAll('.mui-slider-group2 .mui-scroll2'), function(index, pullRefreshEl) {
            $(pullRefreshEl).pullToRefresh({
                down: {
                    callback: function() {
                        this.endPullDownToRefresh();
                    }
                },
                up: {
                    callback: function() {
                        if(!isPullLoading){
                            mui.toast('正在加载中....');
                            isPullLoading = true;
                            var self = this;
                            var currentType = document.getElementById('currentType');
                            var currentPage = document.getElementById('currentPage');
                            var type = currentType.value;
                            var page = Number(currentPage.value);
                            var url = "<?php echo addons_url('WeVote://Home/getCandidatesAction');?>";
                            var actionId = document.getElementById('currentActionId');
                            var data = {
                                type: type,
                                page: page + 1,
                                rows: Number("<?php echo ($rows); ?>"),
                                action_id: actionId.value
                            };
                            $.getJSON(url, data, function(result){
                                if(result.data.length > 0){
                                    currentPage.value = result.currentPage;
                                    var ul = self.element.querySelector('.mui-table-view2');
                                    ul.appendChild(createFragment(ul, result.data));
                                    self.endPullUpToRefresh();
                                }
                                isPullLoading = false;
                            });
                        }
                        this.endPullUpToRefresh();
                    }
                }
            });
        });
        var createFragment = function(ul, data) {
            var fragment = document.createDocumentFragment();
            var li, a, img, muiMediaBody, buttonDiv, button, text, src, input;
            for (var i = 0; i < data.length; i++) {
                var info = data[i];
                var title = info.title;
                var count = info.count;
                var picture = info.picture;
                var id = info.id;

                li = document.createElement('li');
                var mod = i % 2;
                if(mod == 0){
                    li.className = 'mui-table-view-cell mui-media list-width right-margin';
                }
                else{
                    li.className = 'mui-table-view-cell mui-media list-width';
                }
                //a
                a = document.createElement('a');
                var href = document.createAttribute('href');
                href.value = "<?php echo addons_url('WeVote://Home/detail', array('action_id' => $action_id), false);?>/id/" + id + ".html";
                a.setAttributeNode(href);
                // img
                img = document.createElement('img');
                img.className = 'mui-media-object';
                src = document.createAttribute('src');
                src.value = picture;
                img.setAttributeNode(src);
                a.appendChild(img);
                // div mui-media-body
                muiMediaBody = document.createElement('muiMediaBody');
                muiMediaBody.className = 'mui-media-body list-title';
                text = document.createTextNode(title);
                muiMediaBody.appendChild(text);
                a.appendChild(muiMediaBody);
                // div
                buttonDiv = document.createElement('buttonDiv');
                // button
                button = document.createElement('button');
                button.className = 'bt-list VoteButton';
                // input id
                input = document.createElement('input');
                var type = document.createAttribute('type');
                type.value = 'hidden';
                var name = document.createAttribute('name');
                name.value = 'id';
                input.value = id;
                input.className = 'id';
                input.setAttributeNode(type);
                input.setAttributeNode(name);
                button.appendChild(input);
                // input count
                input = document.createElement('input');
                type = document.createAttribute('type');
                type.value = 'hidden';
                name = document.createAttribute('name');
                name.value = 'count';
                input.value = count;
                input.className = 'count';
                input.setAttributeNode(type);
                input.setAttributeNode(name);
                button.appendChild(input);

                text = document.createTextNode('投票');
                button.appendChild(text);
                button.addEventListener('tap', voteButtonHandler);
                buttonDiv.appendChild(button);

                // span countClass
                var countSpan = document.createElement('span');
                countSpan.className = 'bt-data countClass';
                countSpan.innerHTML = count + '票';
                buttonDiv.appendChild(countSpan);

                li.appendChild(a);
                li.appendChild(buttonDiv);
                fragment.appendChild(li);
            }
            return fragment;
        };
    }

    wx.config({
        debug: false,
        appId: '<?php echo ($signPackage["appId"]); ?>',
        timestamp: '<?php echo ($signPackage["timestamp"]); ?>',
        nonceStr: '<?php echo ($signPackage["nonceStr"]); ?>',
        signature: '<?php echo ($signPackage["signature"]); ?>',
        jsApiList: [
            'checkJsApi',
            'onMenuShareTimeline',
            'onMenuShareAppMessage'
        ]
    });

    wx.ready(function () {
        wx.checkJsApi({
            jsApiList: [
                'getNetworkType',
                'previewImage'
            ],
            success: function (res) {
            }
        });

        var shareObj = {
            title: '<?php echo ($shareTitle); ?>',
            desc: '<?php echo ($shareDesc); ?>',
            link: '<?php echo ($shareUrl); ?>',
            imgUrl: '<?php echo ($shareImageUrl); ?>',
            trigger: function (res) {
            },
            success: function (res) {
                var data = {
                    action_id: '<?php echo ($action_id); ?>'
                };

                $.getJSON('<?php echo ($share_url); ?>', data, function(result){
                    if (result.status == 0) {
                        mui.toast('分享成功');
                    }
                });
            },
            cancel: function (res) {
            },
            fail: function (res) {
            }
        };

        //分享朋友
        wx.onMenuShareAppMessage(shareObj);

        //分享朋友圈
        wx.onMenuShareTimeline(shareObj);
    });

    wx.error(function (res) {
        var str = res.errMsg;
    });

    // 投票处理函数
    function voteButtonHandler(e){
        var actionId = document.getElementById('currentActionId');
        var id = this.getElementsByClassName('id')[0];
        var count = this.getElementsByClassName('count')[0];
        var url = "<?php echo addons_url('WeVote://Home/voteAction');?>";
        var data = {
            candidate_id: id.value,
            action_id: actionId.value
        };
        var self = this;
        $.getJSON(url, data, function(result){
            if (result.status) {
                mui.toast(result.info);
                var countSpan = self.parentNode.getElementsByClassName('countClass')[0];
                countSpan.innerHTML = ++count.value + '票';
            } else {
                mui.toast(result.info);
            }
        });
    }
</script>
</html>